﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DBBaseServer.Configurations;
using DBBaseServer.Interfaces;
using SqlSugar;

namespace DBBaseServer.Services
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
    {
        private readonly SqlSugarClient _db;

        public BaseRepository()
        {
            _db = SqlSugarConfiguration.GetInstance();
        }

        public List<T> Query()
        {
            return  _db.Queryable<T>().ToList();
        }

        public async Task<T> GetByIdAsync(int id)
        {
            return await _db.Queryable<T>().InSingleAsync(id);
        }

        public async Task InsertAsync(T entity)
        {
            await _db.Insertable(entity).ExecuteCommandAsync();
        }

        public async Task UpdateAsync(T entity)
        {
            await _db.Updateable(entity).ExecuteCommandAsync();
        }

        public async Task DeleteAsync(int id)
        {
            await _db.Deleteable<T>().In(id).ExecuteCommandAsync();
        }
    }
}
